package com.lyy.service.impl;

import com.lyy.entity.Order;
import com.lyy.feign.ProductFeign;
import com.lyy.mapper.OrderMapper;
import com.lyy.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

/**
 * @author Liangyaoyun
 */

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductFeign productFeign;

    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public int insert(Order order) {

        // 添加订单表记录
        if (orderMapper.insert(order) <= 0) {
            throw new RuntimeException("订单记录添加失败");
        }

        // 远程调用扣减库存
        if (productFeign.updateProduct(order.getProductId(), order.getNumber()) <= 0) {
            throw new RuntimeException("扣减库存失败");
        }

        return 1;
    }
}